//
//  MessageViewController.m
//  LoveDraw
//
//  Created by 零零壹 on 2017/9/25.
//  Copyright © 2017年 梁晨. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MessageModel.h"


#import "AdvDetailViewController.h" // 校园公告详情
#import "TeacherMessageListViewController.h"// 教师留言
#import "MessageListViewController.h" // 家长留言
#import "GrowthRecordViewController.h" // 成长记录
#import "RecordViewController.h" // 点名记录
#import "LessonDetailViewController.h"// 课程详情
#import "HoildayViewController.h"
#import "AdVViewController.h"
#import "TeacherHoildayViewController.h"

@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page;
}
@property(nonatomic,strong)UITableView *listV;
@property(nonatomic,strong)NSMutableArray *listArr;

@end

@implementation MessageViewController

-(NSMutableArray *)listArr
{
    if (!_listArr) {
        _listArr = [NSMutableArray array];
    }
    return _listArr;
}

-(UITableView *)listV
{
    if (!_listV)
    {
        _listV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KscreenW, KscreenH-64) style:(UITableViewStyleGrouped)];
        _listV.delegate = self;
        _listV.dataSource = self;
        _listV.estimatedSectionHeaderHeight = 0;
        _listV.estimatedSectionFooterHeight = 0;
        [_listV registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"MessageTableViewCell"];
    }
    return _listV;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"通知";
    page = 1;
    [self.view addSubview:self.listV];
    self.listV.estimatedRowHeight = 50;
    self.listV.estimatedSectionFooterHeight = 0;
    self.listV.estimatedSectionHeaderHeight = 0;
    self.listV.separatorStyle = 0;
    self.listV.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self request];
    }];
    self.listV.mj_header = [MJRefreshNormalHeader  headerWithRefreshingBlock:^{
        [self doRefreshSenior];
    }];
    [self.listV.mj_header beginRefreshing];
}
-(void)doRefreshSenior
{
    page = 1;
    [self request];
}

-(void)request
{
    [RequestManager RequstWithUrl:@"/index.php?g=App&m=JpushLog&a=jpushLogs" parmDic:@{@"page":[NSString stringWithFormat:@"%ld",page]} requestType:RequestGET Success:^(id data)
    {

        NSArray *Arr = [MessageModel modelConfigure:data];
        if (page == 1) {
            [self.listArr removeAllObjects];
        }
        if (Arr.count==0) {
            page--;
        }
        [self.listArr addObjectsFromArray:Arr];
        [self.listV reloadData];
        [self.listV.mj_footer endRefreshing];
        [self.listV.mj_header endRefreshing];
        if (Arr.count == 0||Arr.count<10)
        {
            self.listV.mj_footer.state = MJRefreshStateNoMoreData;
        }
    } error:^(NSError *error) {

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be cgrecreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return FLT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return FLT_MIN;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listArr.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel *model = self.listArr[indexPath.section];
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCell" forIndexPath:indexPath];
    cell.titleL.text = model.title;
    cell.detailL.text = model.contents;
    cell.typeimageV.layer.cornerRadius = 5;
    cell.typeimageV.layer.masksToBounds = YES;
    cell.dateL.text = model.addtime;
    cell.backV.layer.cornerRadius = 5;
    cell.backV.layer.masksToBounds = YES;
    cell.typeimageV.textColor = [UIColor whiteColor];
    
    if ([model.status isEqualToString:@"1"]) {
        cell.typeimageV.text = @"未读";
        cell.typeimageV.backgroundColor = UIColors(0xFF0000);
        [cell.typeimageV setHidden:NO];

    }else
    {
         cell.typeimageV.text = @"已读";
        cell.typeimageV.backgroundColor = UIColors(0xFF0000);
        [cell.typeimageV setHidden:YES];
    }
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = 0;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageModel *model = self.listArr[indexPath.section];

    if ([model.status isEqualToString:@"1"]) {
        [RequestManager RequstWithUrl:@"/index.php?g=App&m=JpushLog&a=markRead" parmDic:@{@"aid":model.aid} requestType:RequestPOST Success:^(id data)
         {
             model.status = @"2";
             [self.listV reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];

         } error:^(NSError *error) {

         }];
    }

    UIViewController *VC;
     NSInteger index = [model.type integerValue];

    NSLog(@"%@",model.type);


    if (index == 1)
    {
        // 公告推送
        AdvDetailViewController *Adv = [[AdvDetailViewController alloc]init];
        Adv.aid = model.pid;
        VC = Adv;
    }else if (index == 3)
    {

        // 老师留言
        TeacherMessageListViewController *teacherList = [[TeacherMessageListViewController alloc]init];
        teacherList.sid = model.pid;

        VC = teacherList;
    }else if (index == 2)
    {

        // 家长留言
        MessageListViewController *Messgelist = [[MessageListViewController alloc]init];
        Messgelist.caid = model.pid;
        VC = Messgelist;

    }else if (index == 4)
    {
        // 学习小结
        [RequestManager RequstWithUrl:@"/index.php?g=App&m=StudentCourse&a=index" parmDic:@{@"uid":[[NSUserDefaults standardUserDefaults]objectForKey:kUserId],@"access_token":[[NSUserDefaults standardUserDefaults]objectForKey:kAccessToken]} requestType:RequestGET Success:^(id data)
         {
             NSArray *dataArr = data;
             for (NSDictionary *lessonDic in dataArr)
             {
                 if ([lessonDic[@"aid"]isEqualToString:model.pid])
                 {

                     LessonDetailViewController *VC = [[LessonDetailViewController alloc]init];
                     VC.sid = lessonDic[@"aid"];
                     VC.cid = lessonDic[@"cid"];
                     VC.catethumb = lessonDic[@"catethumb"];
                     VC.teachername = lessonDic[@"teachername"];
                     VC.coursename = lessonDic[@"coursename"];
                     [self.navigationController pushViewController:VC animated:YES];
                 }
             }

         } error:^(NSError *error) {

         }];

        return;

    }else if (index == 5)
    {
        // 成长记录
        GrowthRecordViewController *GrowthVC = [[GrowthRecordViewController alloc]init];
        GrowthVC.sid = model.pid;
        VC = GrowthVC;

    }else if (index == 6)
    {
        // 上课通知
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;

    }else if (index == 7)
    {
        // 课费通知
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;

    }else if (index == 8)
    {
        //课时到期
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;

    }else if (index == 9)
    {
        // 老师上课
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;

    }else if (index == 11)
    {
        // 点名通知
        RecordViewController *RecordVC = [[RecordViewController alloc]init];
        RecordVC.aid = model.pid;
        VC =  RecordVC;
    }else if(index == 12)
    {
        HoildayViewController *hoildayVC = [[HoildayViewController alloc]init];
        VC = hoildayVC;
    }else if (index == 13)
    {
        TeacherHoildayViewController *TeaHoilday = [[TeacherHoildayViewController alloc]init];
        VC = TeaHoilday;
    }
    [self.navigationController pushViewController:VC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
